package com.um.jdy.mp.station.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.charge.enums.OrderEnum;
import com.um.jdy.common.device.code.DeviceCode;
import com.um.jdy.common.device.entity.po.Fee;
import com.um.jdy.common.device.entity.po.FeeEbPower;
import com.um.jdy.common.device.entity.po.FeeEbPrice;
import com.um.jdy.common.device.entity.po.FeeEbTime;
import com.um.jdy.common.device.entity.vo.FeeEbPowerVO;
import com.um.jdy.common.device.entity.vo.FeeEbPriceVO;
import com.um.jdy.common.device.entity.vo.FeeEbTimeVO;
import com.um.jdy.common.device.service.FeeEbPowerService;
import com.um.jdy.common.device.service.FeeEbPriceService;
import com.um.jdy.common.device.service.FeeEbTimeService;
import com.um.jdy.common.device.service.FeeService;
import com.um.jdy.mp.station.entity.dto.FeeEbDTO;
import com.um.jdy.mp.station.entity.vo.FeeEbVO;
import com.um.jdy.mp.user.service.SessionUserService;
import com.um.jdy.rpc.enums.DeviceTypeEnum;
import com.um.springboot.starter.code.CommonCode;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class FeeEbManager extends BaseManager<FeeEbPriceService> {

    @Autowired
    SessionUserService sessionUserService;

    @Autowired
    FeeService feeService;

    @Autowired
    FeeEbPowerService feeEbPowerService;

    @Autowired
    private FeeEbTimeService feeEbTimeService;

    public FeeEbVO get(String id) {

        checkPermission(id);

        Fee fee = feeService.getEntity("device_fee_id", id);

        List<FeeEbPrice> priceList = service.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"price"}, new String[]{"asc"});
        List<FeeEbPriceVO> prices = UMBeanUtils.copyBeanList(priceList,FeeEbPriceVO.class);


        List<FeeEbPower> powerList = feeEbPowerService.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"start_power"}, new String[]{"asc"});
        List<FeeEbPowerVO> powers = UMBeanUtils.copyBeanList(powerList,FeeEbPowerVO.class);

        List<FeeEbTime> timeList = feeEbTimeService.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"start_time"}, new String[]{"asc"});
        List<FeeEbTimeVO> times = UMBeanUtils.copyBeanList(timeList,FeeEbTimeVO.class);

        FeeEbVO feeEbVO = new FeeEbVO();
        feeEbVO.setName(fee.getName());
        feeEbVO.setRemark(fee.getRemark());
        feeEbVO.setFeeType(fee.getFeeType());
        feeEbVO.setEnergyPrice(fee.getEnergyPrice());
        feeEbVO.setPlatformMerchantId(feeEbVO.getPlatformMerchantId());
        feeEbVO.setPrices(prices);
        feeEbVO.setPowers(powers);
        feeEbVO.setTimes(times);
        return feeEbVO;
    }

    @Transactional
    public Response add(FeeEbDTO feeEbDTO) {
        Fee fee = UMBeanUtils.copyBean(feeEbDTO, Fee.class);
        fee.setCreateTime(new Date());
        fee.setDeviceType(DeviceTypeEnum.EB.getKey());
        fee.setPlatformMerchantId(sessionUserService.getTokenUserId());
        feeService.addEntity(fee);

        //保存新价格列表
        feeEbDTO.getPrices().stream().forEach(priceDTO -> {
            priceDTO.setDeviceFeeId(fee.getDeviceFeeId());
            service.add(BeanUtil.toBean(priceDTO, FeeEbPrice.class));
        });

        if(StringUtil.equals(fee.getFeeType(), OrderEnum.FeeType.Time.name())) {
            //保存新功率列表
            feeEbDTO.getPowers().stream().forEach(FeePowerDTO -> {
                FeePowerDTO.setDeviceFeeId(fee.getDeviceFeeId());
                feeEbPowerService.add(BeanUtil.toBean(FeePowerDTO, FeeEbPower.class));
            });
        }

        if(StringUtil.equals(fee.getFeeType(), OrderEnum.FeeType.Energy.name())) {
            //保存时段电价列表
            feeEbDTO.getTimes().stream().forEach(feeEbTimeDTO -> {
                feeEbTimeDTO.setDeviceFeeId(fee.getDeviceFeeId());
                feeEbTimeService.add(BeanUtil.toBean(feeEbTimeDTO, FeeEbTime.class));
            });
        }

        return Response.success();
    }

    @Transactional
    public Response update(String id, FeeEbDTO feeEbDTO) {
        Fee fee = UMBeanUtils.copyBean(feeEbDTO,Fee.class);
        fee.setDeviceFeeId(id);
        fee.setDeviceType(DeviceTypeEnum.EB.getKey());
        fee.setUpdateTime(new Date());
        fee.setPlatformMerchantId(sessionUserService.getTokenUserId());
        feeService.updateEntity(fee);

        //删除原价格列表
        service.delete("device_fee_id", id);
        //保存新价格列表
        feeEbDTO.getPrices().stream().forEach(priceDTO -> {
            priceDTO.setDeviceFeeId(id);
            service.save(BeanUtil.toBean(priceDTO, FeeEbPrice.class));
        });

        if(StringUtil.equals(fee.getFeeType(), OrderEnum.FeeType.Time.name())) {
            feeEbTimeService.delete("device_fee_id", id);

            //删除原功率列表
            feeEbPowerService.delete("device_fee_id", id);
            //保存新功率列表
            feeEbDTO.getPowers().stream().forEach(FeePowerDTO -> {
                FeePowerDTO.setDeviceFeeId(id);
                feeEbPowerService.save(BeanUtil.toBean(FeePowerDTO, FeeEbPower.class));
            });
        }

        if(StringUtil.equals(fee.getFeeType(), OrderEnum.FeeType.Energy.name())) {
            feeEbPowerService.delete("device_fee_id", id);

            //删除原时段列表
            feeEbTimeService.delete("device_fee_id", id);
            //保存时段电价列表
            feeEbDTO.getTimes().stream().forEach(feeEbTimeDTO -> {
                feeEbTimeDTO.setDeviceFeeId(id);
                feeEbTimeService.add(BeanUtil.toBean(feeEbTimeDTO, FeeEbTime.class));
            });
        }

        return Response.success();
    }

    /**
     * 校验数据权限
     * @param id
     */
    private void checkPermission(String id){
        Fee fee = feeService.getEntity(id);
        if(fee == null)
            throw new ServiceException(DeviceCode.NoFee);

        if(!StringUtil.equals(sessionUserService.getTokenUserId(),fee.getPlatformMerchantId()))
            throw new ServiceException(CommonCode.NoDataPermission);
    }
}
